from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import time
from pynput import keyboard,mouse
import threading
is_pressed = False
change_mode = False
screen_x = 1366
screen_y = 768
dst_x = round(1366*0.15)
dst_y = round(768*0.15)
show_x = 0
show_y = 0
do_exit = False


def on_click(x,y, button, pressed):  
    print("on click",x,y,button,pressed) 
    global is_pressed
    if 'left' in str(button):
        print('left click')
        if pressed:
            is_pressed = True
        else:
            is_pressed = False

def on_press(key):    
    print(str(key))
    global change_mode,do_exit
    if is_pressed and 'x' in str(key):
        do_exit = True
    if is_pressed and ('z' in str(key) or 'ctrl' in str(key)):
        time.sleep(0.2)
        change_mode = True

listener = keyboard.Listener(
    on_press=on_press
)   
listener.start()
mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()
img2 = None
def get_screen_shot():                                        
        global img2
        screen  =  QGuiApplication.primaryScreen()        
        img = screen.grabWindow(0,0, 0, screen_x, screen_y)
        img2 = img.scaled(dst_x, dst_y, Qt.IgnoreAspectRatio,1);                
class my66(QMainWindow):
    def __init__(self):
        super().__init__()        
        self.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.FramelessWindowHint|Qt.ToolTip)
        self.setWindowTitle('jump-pressed')
        t = QTimer(self)
        t.timeout.connect(self.ont)
        t.start(100)       
        get_screen_shot()

    def paintEvent(self,e):                 
        p = QPainter(self) 
        p.drawPixmap(0,0,dst_x,dst_y,img2)

    def ont(self):
        global change_mode
        if do_exit:
            a.exit()
        if not change_mode:
            return
        change_mode = False
        if self.isHidden():
            get_screen_shot()
            x,y = mouse.Controller().position
            global show_x,show_y
            show_x = max(0,min(screen_x-dst_x-50,x-dst_x//2))
            show_y = max(0,min(screen_y-dst_y-50,y-dst_y//2))
            self.setGeometry(show_x,show_y,dst_x,dst_y)
            self.show()
        else:
            x,y = mouse.Controller().position
            gx = x-show_x
            gy = y-show_y
            if gx>0 and gx<dst_x and gy>0 and gy<dst_y:
                mouse.Controller().position = (round(gx*screen_x/dst_x),round(gy*screen_y/dst_y))
                self.hide() 
a = QApplication([])  
w = my66()
a.exec_()